package lg.minecraft.plugin.testeconomy.storage.model;

import lg.minecraft.plugin.testeconomy.TestEconomy;
import lg.minecraft.plugin.testeconomy.storage.DataBaseStorage;
import lg.minecraft.plugin.testeconomy.storage.StorageType;

import java.io.File;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author langle__
 * @version 1.0
 */
public class SQLiteStorage extends DataBaseStorage {

    private Connection connection;

    public SQLiteStorage() {
        File file = new File(TestEconomy.getManiConfig().getSqlitePath());
        try {
            this.connection = DriverManager.getConnection("jdbc:sqlite:/" + file.getAbsolutePath());
            this.connection.setAutoCommit(true);
            try (Statement statement = connection.createStatement()) {
                statement.executeUpdate("CREATE TABLE IF NOT EXISTS money (player_name VARCHAR(255) PRIMARY KEY, amount DOUBLE)");
            }
        } catch (SQLException e) {
            TestEconomy.getInstance().getLogger().severe("Failed to connect to SQLite database.");
        }
    }

    @Override
    public Connection getConnection() {
        return this.connection;
    }

    @Override
    public StorageType getType() {
        return StorageType.SQLITE;
    }

    @Override
    protected Double getMoney(String playerName) {
        String sql = "SELECT amount FROM money WHERE player_name = ?";
        try (PreparedStatement prepared = getConnection().prepareStatement(sql)) {
            prepared.setString(1, playerName);
            ResultSet resultSet = prepared.executeQuery();
            if (resultSet.next()) {
                return resultSet.getDouble("amount");
            }
        } catch (SQLException e) {
            TestEconomy.getInstance().getLogger().warning("Failed to get money for player " + playerName);
        }
        return null;
    }

    @Override
    protected void setMoney(String playerName, double amount) {
        String sql = "INSERT OR REPLACE INTO money (player_name, amount) VALUES (?, ?)";
        try (PreparedStatement prepared = getConnection().prepareStatement(sql)) {
            prepared.setString(1, playerName);
            prepared.setDouble(2, amount);
            prepared.executeUpdate();
        } catch (SQLException e) {
            TestEconomy.getInstance().getLogger().warning("Failed to set money for player " + playerName);
        }
    }

    @Override
    protected List<String> getPlayers() {
        List<String> players = new ArrayList<>();
        String sql = "SELECT player_name FROM money";
        try (PreparedStatement prepared = getConnection().prepareStatement(sql)) {
            ResultSet resultSet = prepared.executeQuery();
            while (resultSet.next()) {
                players.add(resultSet.getString("playerName"));
            }
        } catch (SQLException e) {
            TestEconomy.getInstance().getLogger().warning("Failed to get players list");
        }
        return players;
    }

}
